﻿using System.ComponentModel;
using Macad.Core;
using Macad.Core.Topology;
using Macad.Interaction;
using Macad.Occt;
using SpCad.Utils;

namespace SpCad
{
    public class SpServer
    {
        #region Static Server
        static SpServer _server;
        public static SpServer GetServer() => Instance;
        public static SpServer Instance => _server ??= (_server = new SpServer());
        #endregion
        public InteractiveContext Current => InteractiveContext.Current;
        public WorkspaceController WorkspaceController => Current.WorkspaceController;
        /// <summary>
        /// 提交更改
        /// </summary>
        public void Commit() => InteractiveContext.Current.UndoHandler.Commit();
        public event Action<object?, PropertyChangedEventArgs>? PropertyChanged;
        /// <summary>
        /// 选择的模型
        /// </summary>
        public Body? SelectBody
        {

            get
            {
                return InteractiveContext.Current.WorkspaceController.Selection.SelectedEntities.Count > 0 ? 
                    InteractiveContext.Current.WorkspaceController.Selection.SelectedEntities.FirstOrDefault(defaultValue:null) as Body : null;
             
            }
        }
      
        /// <summary>
        /// 当前基准面
        /// </summary>
        public Pln WorkingPlane => Current.Workspace.WorkingPlane;

        public void init()
        {
            if (WorkspaceController != null) {
              
                WorkspaceController.PropertyChanged += WorkspaceController_PropertyChanged;
                Current.PropertyChanged += Current_PropertyChanged;
            }
        }

        private void Current_PropertyChanged(object? sender, PropertyChangedEventArgs e)
        {
             
        }

        private void WorkspaceController_PropertyChanged(object? sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName is not "CursorPosition" and not "CursorPosition2d"  )
            {
                PropertyChanged?.Invoke(sender, e);
            }
        }
       
        /// <summary>
        /// 启动Tools
        /// </summary>
        /// <param name="tools"></param>
        /// <param name="action"></param>
        public void StartTools(Tool tools, Action<object, PropertyChangedEventArgs>  action = null)
        {
            try
            {
                WorkspaceController.StartTool(tools);
                if (WorkspaceController.CurrentTool != null)
                {
                    WorkspaceController.CurrentTool.PropertyChanged +=
                  (s, e) =>
                  {
                      action?.Invoke(s, e);
                  };
                }
              
            }
            catch (Exception){  }
        }

        /// <summary>
        /// 消息日志
        /// </summary>
        /// <param name="str"></param>
        public void MsgLog(string str)
        {
            Messages.Info(str);
        }
        /// <summary>
        /// 获取当前的工具
        /// </summary>
        /// <returns></returns>
        public Tool GetTools()
        {
          return  WorkspaceController.CurrentTool;
        }
        
        public void RemoveHintMessage()
        {
            WorkspaceController.HudManager?.SetHintMessage(this, null);
        }

        //--------------------------------------------------------------------------------------------------

        public void SetHintMessage(string message)
        {
            WorkspaceController.HudManager?.SetHintMessage(this, message);
        }
    }
}
